package com.imooc.LinkedList.removeLinkedListElements;

public class Solution2 {

    public ListNode removeElements(ListNode head, int val) {
        // 使用虚拟头结点，这样所有的结点都有前一个结点
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;

        ListNode pre = dummyHead;
        while(pre.next != null) {
            if(pre.next.val == val) {
                ListNode delNode = pre.next;
                pre.next = delNode.next;
                delNode.next = null;
            }else
                pre = pre.next;
        }
        return dummyHead.next;
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,3,3,5,6,3};
        ListNode head = new ListNode(arr);
        System.out.println(head);
        Solution2 solution = new Solution2();
        solution.removeElements(head, 3);
        System.out.println(head);
    }
}
